package com.jkxt.tools;

import java.util.Date;
import java.util.Properties;

import javax.annotation.Resource;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.stereotype.Component;


public class SendEmailUtils {
	private static final String FROM = "hlwznzd@126.com";
	
	/**
	 * 发送重设密码链接的邮件
	 */
	public static void sendResetPasswordEmail(String code,String myEmail,String userName) {
		Session session = getSession();
		MimeMessage message = new MimeMessage(session);
		try {
			message.setSubject("找回您的帐户与密码");
			message.setSentDate(new Date());
			message.setFrom(new InternetAddress(FROM));
			message.setRecipient(RecipientType.TO, new InternetAddress(myEmail));
			message.setContent("亲爱的用户 "+userName+"：您好！<br/>&nbsp;&nbsp;&nbsp;&nbsp; 您收到这封这封电子邮件是因为您 (也可能是某人冒充您的名义) 申请了一个新的密码。假如这不是您本人所申请, 请不用理会这封电子邮件, 但是如果您持续收到这类的信件骚扰, 请您尽快联络管理员。<br/>要使用新的密码, 请使用以下链接启用密码:<br/><a href='" + GenerateLinkUtils.generateResetPwdLink(code,myEmail,userName) +" '>点击重新设置密码</a><br/>注意:请您在收到邮件1个小时内使用，否则该链接将会失效。","text/html;charset=utf-8");
			// 发送邮件
			Transport.send(message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Session getSession() {
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.smtp.host", "smtp.126.com");
		props.setProperty("mail.smtp.port", "25");
		props.setProperty("mail.smtp.auth", "true");
		Session session = Session.getInstance(props, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				String password = "jyz2683823";
				return new PasswordAuthentication(FROM, password);
			}
			
		});
		return session;
	}
	
   public static void main(String[] args) {
	   SendEmailUtils sendEmailUtils=new SendEmailUtils();
  }
}